var _api = _interopRequireDefault(require("../../../utils/api")), _util2 = _interopRequireDefault(require("../../../utils/util")), _cardUtil = _interopRequireDefault(require("../../../utils/card-util")), _radarUtil = require("../../../utils/radar-util"), _util3 = _interopRequireDefault(require("./util")), _index = _interopRequireDefault(require("./visitor/index")), _follow = _interopRequireDefault(require("./follow/follow")), _analysis = _interopRequireDefault(require("./analysis/analysis"));

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

function _objectSpread(e) {
    for (var t = 1; t < arguments.length; t++) {
        var a = null != arguments[t] ? arguments[t] : {}, i = Object.keys(a);
        "function" == typeof Object.getOwnPropertySymbols && (i = i.concat(Object.getOwnPropertySymbols(a).filter(function(t) {
            return Object.getOwnPropertyDescriptor(a, t).enumerable;
        }))), i.forEach(function(t) {
            _defineProperty(e, t, a[t]);
        });
    }
    return e;
}

function _defineProperty(t, e, a) {
    return e in t ? Object.defineProperty(t, e, {
        value: a,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[e] = a, t;
}

function _toConsumableArray(t) {
    return _arrayWithoutHoles(t) || _iterableToArray(t) || _nonIterableSpread();
}

function _nonIterableSpread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance");
}

function _iterableToArray(t) {
    if (Symbol.iterator in Object(t) || "[object Arguments]" === Object.prototype.toString.call(t)) return Array.from(t);
}

function _arrayWithoutHoles(t) {
    if (Array.isArray(t)) {
        for (var e = 0, a = new Array(t.length); e < t.length; e++) a[e] = t[e];
        return a;
    }
}

var visitor = (0, _index.default)(), follow = (0, _follow.default)(), analysis = (0, 
_analysis.default)(), tabs = [ visitor, follow, analysis ], app = getApp(), TURNOVER_ARRAY = Array.from(Array(10), function(t, e) {
    return "≥".concat(e += 1, "0%");
});

Page({
    data: {
        TURNOVER_ARRAY: TURNOVER_ARRAY,
        client: {}
    },
    onLoad: function(t) {
        this.query = t, this.setCardData(app.globalData.card), this.initEvent(), this.loadView();
    },
    index: 0,
    onUnload: function() {
        this.labelsUpdate && (this.$emit("niche.lists.label.update"), this.labelsUpdate = !1);
    },
    onPullDownRefresh: function() {
        this.loadView(), tabs[this.index].onPullDownRefresh();
    },
    onReachBottom: function() {
        tabs[this.index].onReachBottom();
    },
    initEvent: function() {
        var a = this;
        [ [ "card.change", this.setCardData.bind(this) ], [ "niche.label.change", function(t, e) {
            a.handleNicheLabelChange(t, e);
        }, !1 ] ].forEach(function(t) {
            return a.$addListener.apply(a, _toConsumableArray(t));
        });
    },
    setCardData: function(t) {
        this.setData({
            card: t
        });
    },
    handleNicheLabelChange: function(t, e) {
        var a = t.type, i = t.oldName, n = void 0 === i ? [] : i, r = t.newName, o = void 0 === r ? [] : r, c = t.id, u = this.data.client, d = u.labels;
        0 === e ? u.labels = d.concat(o) : 2 === e ? (n.forEach(function(t) {
            return d.splice(d.indexOf(t), 1);
        }), u.labels = d.concat(o)) : 1 === e && n.forEach(function(t) {
            var e = d.indexOf(t);
            0 < e && d.splice(e, 1);
        }), u[a] = c, this.labelsUpdate = !0, this.setData({
            client: u
        });
    },
    loadView: function() {
        var a = this, i = this.query;
        return _api.default.minicard.business.view({
            loading: "加载中...",
            data: i
        }).then(function(t) {
            var e = t.data;
            i.fromuid && (e.fromuid = i.fromuid), a.setData({
                client: e,
                isSelf: +e.uid == +a.data.card.uid
            }), a.gaugeCanvas(0 == +e.rate ? 0 : e.rate / 10);
        });
    },
    onNavChange: function(t) {
        var e = t.detail, a = e.index, i = e.reset;
        this.index = a, i && tabs[a].init(this);
    },
    changeTurnover: function(t) {
        var e = t.detail.value;
        e = +e + 1, this.setData({
            "client.rate": 10 * e
        }), this.gaugeCanvas(e), this.updateTurnover(10 * e);
    },
    updateTurnover: function(t) {
        var i = this;
        this.updateData({
            rate: t
        }).then(function() {
            var a = "预计成交率：更改为".concat(t, "%");
            _util3.default.addFollowRecord(_objectSpread({
                content: a
            }, i.query)).then(function(t) {
                var e = t.data;
                return i.$emit("niche.record.update", {
                    content: a,
                    data: e
                });
            });
        });
    },
    updateData: function(t) {
        return _api.default.minicard.business.update({
            data: _objectSpread({}, t, this.query)
        });
    },
    gaugeCanvas: function(t) {
        var e = this, a = this.ctx || (this.ctx = wx.createCanvasContext("gauge")), i = function(t) {
            return Math.PI * t / 180;
        }, n = 0, r = i(115), o = t * i(30.9);
        a.clearRect(0, 0, 82, 82), clearInterval(this.gauge), this.gauge = setInterval(function() {
            var t = r + o / 100 * ++n;
            a.save(), a.setLineWidth(4.2), a.beginPath(), a.arc(_util2.default.calcRpx(82), _util2.default.calcRpx(82), _util2.default.calcRpx(78), r, t), 
            a.setStrokeStyle("#ffffff"), a.stroke(), a.draw(!0), 100 < n && clearInterval(e.gauge);
        }, 20);
    },
    gotoRemark: function() {
        this.$put("client.remark", this.data.client.remark), this.$route("./addRemark/addRemark", this.query);
    },
    addRecord: function() {
        this.$put("niche.add.record", {
            query: this.query
        }), this.$route("./addRecord/addRecord");
    },
    setLabel: function() {
        var t = this.data.client, e = t.type, a = t.status, i = t.labelids;
        this.$route("/pages/radar/nichedetail/setlabel/setlabel", Object.assign({}, this.query, {
            labelids: i,
            type: e,
            status: a
        }));
    },
    handleQueryData: function() {
        var t = this.data.card, e = t ? t.cardid : "", a = this.data.client;
        return {
            name: a.realname,
            touid: a.touid,
            cardid: e
        };
    },
    checkHasExchange: function() {
        if (!this.exchangeCheck) {
            var t = this.data.client.touid;
            this.exchangeCheck = _api.default.minicard.card.checkisexchangecard({
                data: {
                    withuid: t
                }
            }).then(function(t) {
                return t.data.hasExchange;
            });
        }
        return this.exchangeCheck;
    },
    gotoCard: function() {
        var t = this, e = "查看Ta的名片", a = this.data.client, i = a.tocardid, n = a.toguid;
        this.handleExchange(e).then(function() {
            i && n ? t.$route("/pages/card/view/view?guid=".concat(n, "&cardid=").concat(i)) : t.notFoundClientCard(e);
        });
    },
    handleExchange: function(e) {
        var a = this;
        return this.checkHasExchange().then(function(t) {
            return t ? Promise.resolve() : (a.exchangeCardTip(e), Promise.reject());
        });
    },
    exchangeCardTip: function(t) {
        var e = this;
        wx.showModal({
            content: "交换名片后即可".concat(t),
            confirmText: "交换名片",
            success: function(t) {
                t.confirm && e.exchangeCardSend();
            }
        });
    },
    exchangeCardSend: function() {
        var t = this.data.client, e = this.$appData.card;
        _cardUtil.default.exchangeCard({
            uid: t.touid
        }, e), wx.showModal({
            content: "已申请交换名片，对方同意后即可查看Ta的名片",
            showCancel: !1,
            confirmText: "知道了"
        });
    },
    goChat: function() {
        var t = this.handleQueryData();
        if (!_util2.default.checkIsToChat(_objectSpread({}, t))) {
            var e = t.name, a = t.cardid, i = t.touid;
            (0, _radarUtil.gotoChatRoom)({
                name: e,
                cardid: a,
                touid: i
            });
        }
    },
    callUp: function() {
        var r = this;
        this.handleExchange("拨打电话").then(function() {
            var t = r.data.client, e = t.mobile, a = t.cardid, i = t.touid, n = t.guid;
            e ? (wx.makePhoneCall({
                phoneNumber: e
            }), (0, _radarUtil.radarPush)({
                cardid: a,
                guid: n,
                uid: i
            }, _radarUtil.RADAR_TYPES.CALL)) : r.notFoundClientCard("拨打电话");
        });
    },
    notFoundClientCard: function(t) {
        wx.showModal({
            content: "该客户还没有创建名片，暂不支持".concat(t),
            showCancel: !1,
            confirmText: "知道了"
        });
    },
    previewAvatar: function() {
        wx.previewImage({
            urls: [ this.data.client.avatar ]
        });
    },
    deliverNiche: function() {
        var t = this.data, e = t.card, a = e.guid, i = e.cardid, n = t.client, r = n.touid, o = n.desc, c = n.realname, u = o || c;
        this.$route("/pages/contacts/userselect/userselect?action=deliverNicheClient&guid=".concat(a, "&cardid=").concat(i, "&businessuid=").concat(r, "&clientName=").concat(u));
    },
    receiveCount: function(t) {
        this.setData({
            visitorCount: t.detail.count
        });
    }
});